package com.vader.boot.shortUrl;

import com.vader.boot.utils.ShortUrlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Vader
 * @date 2022/4/28
 **/
@RestController
@RequestMapping("/short")
@Slf4j
public class ShortUrlController {

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/URLEncode")
    public String URLEncode(@RequestBody String url){
        List<String> shortUrlList = ShortUrlUtils.getShortUrl(url);
        String shortUrl = shortUrlList.get(new Random().nextInt(4));
        redisTemplate.opsForHash().put("shortUrl:key",shortUrl,url);
        return shortUrl;
    }

    @PostMapping("/URLDecode")
    public String URLDecode(@RequestBody String shortUrl){
        Object url = redisTemplate.opsForHash().get("shortUrl:key", shortUrl);
        return String.valueOf(url);
    }


}
